package com.lgj.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lgj.mall.common.utils.PageUtils;
import com.lgj.mall.common.utils.R;

import com.lgj.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lgj.mall.product.entity.AttrEntity;
import com.lgj.mall.product.service.AttrAttrgroupRelationService;
import com.lgj.mall.product.service.AttrService;
import com.lgj.mall.product.service.CategoryService;
import com.lgj.mall.product.vo.AttrGroupRelationVO;
import com.lgj.mall.product.vo.AttrGroupWithAttrsVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lgj.mall.product.entity.AttrGroupEntity;
import com.lgj.mall.product.service.AttrGroupService;




/**
 * 属性分组
 *
 * @author lgj
 * @email 3233744083@qq.com
 * @date 2025-07-05 15:58:48
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
//    @Autowired
//    private AttrService attrService;
    private final AttrService attrService;
    @Autowired
    public AttrGroupController(AttrService attrService) {
        this.attrService = attrService;
    }
    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    //获取属性分组关联的所有属性
    @GetMapping("{attrgroupId}/attr/relation")
    public R getAtrrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrs = attrService.getRelationAtrr(attrgroupId);
        return R.ok().put("data",attrs);
    }
    //获取属性分组未关联的所有属性
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoAtrrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String,Object> params){
        PageUtils pageUtils= attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",pageUtils);
    }
    //解除关联
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos){
        attrService.deleteRelationAtrr(vos);

        return R.ok();
    }

    //新增关联属性
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVO> vos = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",vos);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        /**
         * 查找属性对应下的一级、二级、三级分类路径
         */
        Long catelogId= attrGroup.getCatelogId();
        Long[] paths=categoryService.findCatelogPath(catelogId);
        //设置分类路径
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
